﻿namespace com.vs.General
{
    using System;
    using System.Collections;
    using System.Collections.Specialized;
    using System.Globalization;
    using System.Text;

    public class StringManager
    {
        public static string CollectionToString(ICollection c)
        {
            StringBuilder builder = new StringBuilder();
            if (c != null)
            {
                ArrayList list = new ArrayList(c);
                bool flag = (((c is BitArray) || (c is Hashtable)) || ((c is IDictionary) || (c is NameValueCollection))) || ((list.Count > 0) && (list[0] is DictionaryEntry));
                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i] == null)
                    {
                        builder.Append("null");
                    }
                    else if (!flag)
                    {
                        builder.Append(list[i]);
                    }
                    else
                    {
                        DictionaryEntry entry;
                        flag = true;
                        if (c is NameValueCollection)
                        {
                            builder.Append(((NameValueCollection) c).GetKey(i));
                        }
                        else
                        {
                            entry = (DictionaryEntry) list[i];
                            builder.Append(entry.Key);
                        }
                        builder.Append("=");
                        if (c is NameValueCollection)
                        {
                            builder.Append(((NameValueCollection) c).GetValues(i)[0]);
                        }
                        else
                        {
                            entry = (DictionaryEntry) list[i];
                            builder.Append(entry.Value);
                        }
                    }
                    if (i < (list.Count - 1))
                    {
                        builder.Append(", ");
                    }
                }
                if (flag && (c is ArrayList))
                {
                    flag = false;
                }
                if (flag)
                {
                    builder.Insert(0, "{");
                    builder.Append("}");
                }
                else
                {
                    builder.Insert(0, "[");
                    builder.Append("]");
                }
            }
            else
            {
                builder.Insert(0, "null");
            }
            return builder.ToString();
        }

        public static bool Equals(object a, object b)
        {
            return Equals(a as string, b as string, true);
        }

        public static bool Equals(string a, string b)
        {
            return Equals(a, b, true);
        }

        public static bool Equals(string a, string b, bool ignoreCase)
        {
            if (IsEmpty(a) && IsEmpty(b))
            {
                return true;
            }
            if (IsEmpty(a) || IsEmpty(b))
            {
                return false;
            }
            return (string.Compare(a, b, ignoreCase, CultureInfo.InvariantCulture) == 0);
        }

        public static bool IsEmpty(string str)
        {
            return ((str == null) || (string.Compare(str, "", true, CultureInfo.InvariantCulture) == 0));
        }

        public static bool IsEmpty(StringBuilder str)
        {
            return ((str == null) || (str.Length == 0));
        }

        [Obsolete("Please use StringManager.Equals", false)]
        public static bool IsEqual(string a, string b)
        {
            return Equals(a, b, true);
        }

        [Obsolete("Please use StringManager.Equals", false)]
        public static bool IsEqual(string a, string b, bool ignoreCase)
        {
            return Equals(a, b, ignoreCase);
        }

        public static bool IsErrorMessage(string msg)
        {
            return (!IsEmpty(msg) && (msg.StartsWith("MSG") || msg.StartsWith("错误")));
        }

        public static bool IsNumeric(string str)
        {
            if (IsEmpty(str))
            {
                return false;
            }
            for (int i = 0; i < str.Length; i++)
            {
                if (!char.IsDigit(str, i))
                {
                    return false;
                }
            }
            return true;
        }

        public static string Pad(string str, int size, char pad)
        {
            if (str == null)
            {
                str = "";
            }
            while (str.Length < size)
            {
                str = pad + str;
            }
            return str;
        }

        public static string RemoveLeadingCharacters(string st, char c)
        {
            char[] chArray = null;
            int startIndex = 0;
            if ((st == null) || IsEmpty(st))
            {
                return (st ?? "");
            }
            chArray = st.ToCharArray();
            for (int i = 0; (chArray != null) && (i < chArray.Length); i++)
            {
                if (chArray[i] != c)
                {
                    break;
                }
                startIndex++;
            }
            return st.Substring(startIndex);
        }

        public static string RemoveUnderScore(string input)
        {
            if (input.IndexOf("_") == -1)
            {
                return input;
            }
            return (input.Substring(0, input.IndexOf("_")) + RemoveUnderScore(input.Substring(input.IndexOf("_") + 1)));
        }

        public static string ReplaceAll(string val, string match, string replacement)
        {
            if ((!IsEmpty(val) && (match != null)) && (replacement != null))
            {
                return val.Replace(match, replacement);
            }
            return val;
        }

        public static string ReplaceAllRegions(string data, string regionStart, string regionEnd)
        {
            int index = 0;
            int startIndex = 0;
            for (startIndex = data.IndexOf(regionStart); startIndex != -1; startIndex = data.IndexOf(regionStart))
            {
                index = data.IndexOf(regionEnd);
                if ((index == -1) || ((index + regionEnd.Length) <= startIndex))
                {
                    return data;
                }
                data = data.Remove(startIndex, (index + regionEnd.Length) - startIndex);
            }
            return data;
        }

        public static string[] Split(string str, params string[] delimiters)
        {
            if (IsEmpty(str))
            {
                return null;
            }
            for (int i = 0; (delimiters != null) && (i < delimiters.Length); i++)
            {
                str = str.Replace(delimiters[i], new string('\x001e', 1));
            }
            return str.Split(new char[] { '\x001e' });
        }

        public static string ToPascalCase(string columnName)
        {
            StringBuilder builder = new StringBuilder();
            string[] strArray = columnName.Split(new char[] { '_' });
            string str = null;
            for (int i = 0; i < strArray.Length; i++)
            {
                str = strArray[i];
                builder.Append(str.Substring(0, 1).ToUpper());
                builder.Append(str.Substring(1).ToLower());
            }
            return builder.ToString();
        }

        public static string ToSafeSQLString(DateTime dt)
        {
            return ("'" + dt.ToString("yyyy-MM-dd") + "'");
        }

        public static string ToSafeSQLString(string st)
        {
            if (null == st)
            {
                return "NULL";
            }
            return ("'" + st + "'");
        }
    }
}

